#! /usr/bin/sdlbrt
finp$="parrot_crop_160x96.png"
if argc>=3 then:finp$=command$(3):end if

dim bmdmp[32768]

dim dpvl[8]
function hexvl(a$)
   e=asc(ucase$(a$))
   hexvl=abs(((e>=48 and e<=57)*(e-48))+((e>=65 and e<=70)*(e-55)))
 end function
sub hexvpr(a$)
     lngta=len(a$):i=1
  while i<=lngta
     m1$=mid$(a$,i,1):m2$=mid$(a$,i+1,1)
     b=(hexvl(m1$)*16)+hexvl(m2$):WriteByte(1,b):i+=2
   end while
 end sub
function bthx$(a)
  b$="00"+hex$(a):bthx$=right$(b$,2)
 end function

shell("rm _p_.xpm")
shell("touch _p_.xpm")
shell("echo \"/* XPM */\" >> _p_.xpm")
shell("echo \"static char * xpm_xpm[] = {\" >> _p_.xpm")
shell("echo \'\"16 1 16 1\",\' >> _p_.xpm")
shell("echo \'\"0 c #000000\",\' >> _p_.xpm")
shell("echo \'\"1 c #2C00F7\",\' >> _p_.xpm")
shell("echo \'\"2 c #008800\",\' >> _p_.xpm")
shell("echo \'\"3 c #0187FF\",\' >> _p_.xpm")
shell("echo \'\"4 c #AA0000\",\' >> _p_.xpm")
shell("echo \'\"5 c #FF00FD\",\' >> _p_.xpm")
shell("echo \'\"6 c #748904\",\' >> _p_.xpm")
shell("echo \'\"7 c #979996\",\' >> _p_.xpm")
shell("echo \'\"8 c #535553\",\' >> _p_.xpm")
shell("echo \'\"9 c #8688FD\",\' >> _p_.xpm")
shell("echo \'\"A c #00FF00\",\' >> _p_.xpm")
shell("echo \'\"B c #37FFFF\",\' >> _p_.xpm")
shell("echo \'\"C c #FF4253\",\' >> _p_.xpm")
shell("echo \'\"D c #FF98FE\",\' >> _p_.xpm")
shell("echo \'\"E c #F1FF00\",\' >> _p_.xpm")
shell("echo \'\"F c #FFFFFF\",\' >> _p_.xpm")
shell("echo \'\"0123456789ABCDEF\"};\' >> _p_.xpm;")
shell("convert _p_.xpm -scale 400% _t6.png")
shell("convert _p_.xpm -scale 400% _t4.png")

clstr=0 '- 0..3
xed=160:yed=96:wdwd=1
'shell("rm _t*.png")
shell("convert "+finp$+" -scale "+str$(xed*4)+"x"+str$(yed*4)+"! _t2.png")
'shell("convert "+finp$+" +dither -colors 16 _t4.png")
'shell("convert _t4.png +dither -posterize 8 _t5.png")
'shell("convert _t5.png +dither -colors 16 _t6.png")
'shell("convert _paleta.xpm -remap _paleta.xpm _t6.png")
shell("convert _t2.png -quantize CMY -remap _t6.png -dither Riemersma _t3.png")
'shell("convert _t3.png -unique-colors -scale 400% _t4.png")

setdisplay(xed*4,yed*4,32,wdwd):paper(8^8-1):ink(0):pen(0):cls
loadimage("_t3.png",1):pasteicon(0,0,1)

dim p1[16],k1[16]

function grayscale(ct):
  cq=ct
  bq=bitwiseand(cq,255):cq=int(cq/256)
  gq=bitwiseand(cq,255):cq=int(cq/256)
  rq=bitwiseand(cq,255):cq=int(cq/256)
  return int(((rq*30)+(gq*59)+(bq*11))/100)
  end function

for y2=0 to yed-1
  setcaption(str$(int(100*(y2/yed)))+"%")
  for x2=0 to xed-1
    for y1=0 to 3:for x1=0 to 3
      xq=y1*4+x1
      p1[xq]=point(x2*4+x1,y2*4+y1)
      next:next
    for i=0 to 15
      k1[i]=grayscale(p1[i])
      next
    for j=0 to 14:for i=0 to 14
      if k1[i]>k1[i+1] then:
        tm=k1[i]:k1[i]=k1[i+1]:k1[i+1]=tm
        tm=p1[i]:p1[i]=p1[i+1]:p1[i+1]=tm
        end if
      next:next

    if clstr=0 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 6]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 9]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[10]):dot(x2*4+1,y2*4+1)
    ink(p1[ 3]):dot(x2*4+2,y2*4+1)
    ink(p1[13]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[ 7]):dot(x2*4+3,y2*4+2)
    ink(p1[ 2]):dot(x2*4+0,y2*4+3)
    ink(p1[12]):dot(x2*4+1,y2*4+3)
    ink(p1[ 5]):dot(x2*4+2,y2*4+3)
    ink(p1[11]):dot(x2*4+3,y2*4+3)
    end if
    
    if clstr=1 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 4]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 8]):dot(x2*4+2,y2*4+0)
    ink(p1[12]):dot(x2*4+3,y2*4+0)
    ink(p1[10]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 2]):dot(x2*4+2,y2*4+1)
    ink(p1[ 6]):dot(x2*4+3,y2*4+1)
    ink(p1[ 1]):dot(x2*4+0,y2*4+2)
    ink(p1[ 5]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[11]):dot(x2*4+0,y2*4+3)
    ink(p1[15]):dot(x2*4+1,y2*4+3)
    ink(p1[ 3]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=2 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[10]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 1]):dot(x2*4+2,y2*4+0)
    ink(p1[11]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 5]):dot(x2*4+2,y2*4+1)
    ink(p1[15]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[ 2]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[ 3]):dot(x2*4+3,y2*4+2)
    ink(p1[12]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[13]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=3 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[12]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 3]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 8]):dot(x2*4+0,y2*4+1)
    ink(p1[ 4]):dot(x2*4+1,y2*4+1)
    ink(p1[11]):dot(x2*4+2,y2*4+1)
    ink(p1[ 7]):dot(x2*4+3,y2*4+1)
    ink(p1[ 2]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[10]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[ 9]):dot(x2*4+2,y2*4+3)
    ink(p1[ 5]):dot(x2*4+3,y2*4+3)
    end if

    x1=x2 mod 4:y1=y2 mod 4
    u=point(x2*4+x1,y2*4+y1)
    ink(u):dot(x2,y2)
    next:next

loadimage("_t4.png",1):pasteicon(xed,0,1)
shell("rm _t*.png")

grab(1,0,0,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+".png && rm _.bmp" )

for y1=0 to 95
  setcaption(str$(y1)+"/192")
  for x1=0 to 159
    for c=0 to 15
      if point(x1,y1)=point(xed+(c*4),0) then
        bmdmp[x1+(y1*160)]=c
        end if
      next:next:next

setcaption("a...")
for i=0 to ((96*160)/2)-1
  bmdmp[i]=bmdmp[i*2+1]*16+bmdmp[i*2+0]
  next

setcaption("b...")
open finp$+".bin" for output as #1
for i=0 to ((96*160)/2)-1
  writebyte(1,bmdmp[i])
  next
close #1

setcaption("c...")
grab(1,0,0,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+".png && rm _.bmp" )

